package com.nc.shop.service.controller;

import com.nc.shop.service.dao.ShopDao;
import com.nc.shop.service.model.dto.RepertoryDto;
import com.nc.shop.service.model.entity.RepertoryEntity;
import com.nc.shop.service.service.RepertoryService;
import com.plug.BeanUtil;
import com.plug.result.ReqPage;
import com.plug.result.RetResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 创建人:    xvzhe
 * 创建时间:
 * 修改时间:
 * 版本:      v1.0
 */
@RestController
@Api
@RequestMapping("RepertoryController")
public class RepertoryController {
    @Autowired
    RepertoryService repertoryService;
    @Autowired
    ShopDao shopDao;

    @ApiOperation("库存记录")
    @PostMapping("repertory")
    public RetResult getRepertory(@RequestBody ReqPage reqPage) {
        return repertoryService.findPage(reqPage);
    }

    @PutMapping("repertory")
    @ApiOperation("入库")
    public RetResult putRepertory(@RequestBody RepertoryDto repertoryDto) {
        RepertoryEntity repertoryEntity = BeanUtil.convertToBean(repertoryDto, RepertoryEntity.class);
        repertoryEntity.setShopEntity(shopDao.findById(repertoryDto.getShopId()).get());
        return repertoryService.insert(repertoryEntity);
    }

    @DeleteMapping("repertory")
    @ApiOperation("出库")
    public RetResult deleteRepertory(@RequestBody RepertoryDto repertoryDto) {
        RepertoryEntity repertoryEntity = BeanUtil.convertToBean(repertoryDto, RepertoryEntity.class);
        repertoryEntity.setShopEntity(shopDao.findById(repertoryDto.getShopId()).get());
        return repertoryService.out(repertoryEntity);
    }
}
